
import cv2
import logging
import time
import numpy as np
from DailyBase import *
import random


logger = logging.getLogger(__name__)



class BaoTu(DailyBase):

    def __init__(self, enum):
        self.RCName = __name__
        self.LogHeader = self.RCName +str(enum.port)
        self.enum = enum
        self.started = False
        self.DaTuJieShu = False
        self.KaiShiWa = False
        self.CurrBaoGuoYe = 0
        self.loadPrivateActions(self.areaDict, BaoTuActions)

        logger.info("{} has {} actions".format(self.LogHeader, len(self.areaDict)))

    def reset(self):
        self.KaiShiWa = False
        self.DaTuJieShu = False
        self.CurrBaoGuoYe = 0

    def KaiShi(self):
        logger.info(".")

        self.reset()

        if STATUS_NOTFOUND_DAILY == self.begin(isDaily = True):
            logger.info("尝试 挖宝")
            self.reset()
            self.DaTuJieShu = True
            self.begin(isDaily=False)



    def process_private(self, img = None, data = None):

        logger.info("{} KaiShiWa {}, DaTuJieShu {}".format(self.LogHeader, self.KaiShiWa, self.DaTuJieShu))



        if self.DaTuJieShu is False:
            if self.status == STATUS_ZHANDOU_SHOUDONG:
                self.enum.click(ZhanDouZiDong)
                return
            if self.status != STATUS_OTHERS:
                return
            if self.areaDict['JieRenWu'].detect(img) < 20:
                self.enum.click(self.areaDict['JieRenWu'].click, delay=1000)

            if self.areaDict['DaTuJieShu'].detect(img) < 20:
                self.enum.click(self.areaDict['DaTuJieShu'].click, delay=500)
                self.DaTuJieShu = True
            return

        if self.KaiShiWa is False:

            if self.status == STATUS_FEIZHANDOU:
                self.enum.click(openBaoGuo)
                return

            if gPublic.IsWuPinLan(img) == STATUS_OTHERS:
                return

            if self.areaDict['ZhaoBaoTu'].detect(img) < 20:
                self.enum.click(self.areaDict['ZhaoBaoTu'].click, delay = 500)
                self.enum.click(self.areaDict['YongBaoTu'].click)
                self.KaiShiWa = True
                return

            else:
                self.CurrBaoGuoYe += 1
                if self.CurrBaoGuoYe > 2:
                    self.stop()
                    return STATUS_FINISHED
                self.enum.click(BaoGuoYe[self.CurrBaoGuoYe])

            if self.areaDict['YongBaoTu'].detect(img) < 10:
                self.enum.click(self.areaDict['YongBaoTu'].click)
                self.KaiShiWa = True

        else:

            if self.feiZhanDouShu > 10:
                self.stop()
                return STATUS_FINISHED

            if self.areaDict['WaBao'].detect(img) < 10:
                self.enum.click(self.areaDict['WaBao'].click, delay = random.randint(300, 500))
                self.feiZhanDouShu = 0
            elif self.areaDict['XuanBao'].detect(img) < 20:
                self.enum.click(self.areaDict['XuanBao'].click)
                self.feiZhanDouShu = 0



